package bookManage.Controller.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static bookManage.utils.Constant.USER_SESSION;

/**
 * @BelongsProject: library-management-system-master
 * @BelongsPackage: bookManage.Controller.filter
 * @Author: yuan wang
 * @CreateTime: 2023-03-15  10:53
 * @Description: TODO
 * @Version: 1.0
 */
public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        Object userSession = req.getSession().getAttribute(USER_SESSION);

        if (userSession == null) {
            resp.sendRedirect("error.html");
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
